package com.example.score_reception.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.score_reception.entity.Class;
import com.example.score_reception.entity.*;
import com.example.score_reception.service.*;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
/**
 * @author shenrui
 * @date 2021/9/30
 * @description 学生的行为
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    StudentScoreService studentScoreService;
    @Autowired
    ClassService classService;
    @Autowired
    CourseService courseService;
    @Autowired
    ClassStudentService classStudentService;
    /**
     *@description 页面跳转
     * @return String
     */
    @RequestMapping("/student_login")
    public String student() {
        //已经登录用户无需登录
        if(!SecurityContextHolder.getContext().getAuthentication().getName().equals("anonymousUser")) {
            return "redirect:/student/student_main";
        } else {
            return "/student/student_login";
        }
    }
    @RequestMapping("/student_main")
    public String studentMain() {
        return "/student/student_main";
    }

    @RequestMapping("/student_class_list")
    public  String studentCourseList() {
        return "/student/student_class_list";
    }

    @RequestMapping("/student_score_list")
    public  String studentScoreList() {
        return "/student/student_score_list";
    }
    @RequestMapping("/student_password")
    public  String studentPassword() {
        return "/student/student_password";
    }

    /**
     *@description 学生查看自己的成绩
     * @return String
     */
    @RequestMapping("/do_student_score_list")
    public String doStudentScoreList(HttpSession session) {
        Student student = (Student) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<StudentScore> studentScoreList = studentScoreService.list(new LambdaQueryWrapper<StudentScore>().eq(StudentScore::getStudentId,student.getStuId()).eq(StudentScore::getScoreStatus,1));
        Class scoreClass;
        if(studentScoreList != null) {
            for (int i = 0; i < studentScoreList.size(); i++) {
                scoreClass = classService.getOne(new LambdaQueryWrapper<Class>().eq(Class::getId,studentScoreList.get(i).getClassId()));
                studentScoreList.get(i).setIdClass(scoreClass.getClassId());
                studentScoreList.get(i).setCourseName(courseService.getOne(new LambdaQueryWrapper<Course>().eq(Course::getCourseId,scoreClass.getCourseId())).getCourseName());
            }
        }
        session.setAttribute("studentScoreList",studentScoreList);
        return "redirect:/student/student_score_list";
    }

    /**
     *@description 学生查看自己的课程
     * @return String
     */
    @RequestMapping("/do_student_class_list")
    public  String doStudentClassList(HttpSession session) {
        Student student = (Student) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Class> classList = new ArrayList<>();
        Class studentClass;
        List<ClassStudent> classStudentList = classStudentService.list(new LambdaQueryWrapper<ClassStudent>().eq(ClassStudent::getStudentId,student.getStuId()));
        if(classStudentList !=null) {
            for(int i = 0 ; i < classStudentList.size() ; i++) {
                studentClass = classService.getOne(new LambdaQueryWrapper<Class>().eq(Class::getId,classStudentList.get(i).getClassId()));
                studentClass.setCourseName(courseService.getOne(new LambdaQueryWrapper<Course>().eq(Course::getCourseId,studentClass.getCourseId())).getCourseName());
                classList.add(studentClass);
            }
        }
        session.setAttribute("classList",classList);
        return "redirect:/student/student_class_list";
    }

    /**
     *@description 跳转修改密码页面
     * @return String
     */
    @RequestMapping("/do_student_password")
    public String doStudentPassword(HttpSession session) {
        Student student = (Student) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        session.setAttribute("studentInfo",student);
        return "redirect:/student/student_password";
    }

    /**
     *@description 学生修改密码
     * @return String
     */
    @RequestMapping("/submit_stu_password")
    public String submitStudentPassword(Student student, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(student.getStuPassword())) {
            model.addAttribute("stuMsg", "密码不能为空");
            return "redirect:/student/student_password";

        }
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = bcryptPasswordEncoder.encode(student.getStuPassword());
        student.setStuPassword(hashPass);
        //进行更新
        Boolean bool = studentService.updateById(student);
        if (bool) {
            return "redirect:/student/student_main";
        } else {
            model.addAttribute("stuMsg", "修改失败");
            return "redirect:/student/student_password";
        }
    }
}
